<?php
function sendsms($content, $m_type, $mobile, $name)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("sms");
    $m_daipay = $setting[$m_type];
    $wechat = pdo_fetch("select * from " . tablename('account_wechats') . " where uniacid=:uniacid", array(':uniacid' => $_W['uniacid']));
    if ($wechat['smsnum'] > 0 && $m_daipay == 'true' && preg_match("/^1[34578]{1}\d{9}$/", trim($mobile))) {
        //获取今天 本手机号 发送次数
        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
        $smsConf = array(
            'key' => '18666e8bbea7da82366fffd2388623e5', //您申请的APPKEY
            'mobile' => $mobile, //接受短信的用户手机号码
            'tpl_id' => '20283', //尊敬的用户，您#orderno#请注意及时参与！--#app#
            'tpl_value' => '#orderno#=' . $content . '&#app#=' . $_W['uniaccount']['name'] //您设置的模板变量，根据实际情况修改
        );
        $content = juhecurl($sendUrl, $smsConf, 1); //请求发送短信
        if ($content) {
            $result = json_decode($content, true);
            $error_code = $result['error_code'];
            if ($error_code == 0) {
                //$result['result']['fee']  扣除短信条数
                $nowsmsnum = $wechat['smsnum'] - $result['result']['fee'];
                pdo_update('account_wechats', array('smsnum' => $nowsmsnum), array('uniacid' => $_W['uniacid']));
                $data = array(
                    'uniacid' => $_W['uniacid'],
                    'name' => $name,
                    'mobile' => $mobile,
                    'createtime' => time()
                );
                $mob = pdo_fetch("select * from " . tablename('tg_sms_mobile') . " where uniacid=:uniacid and mobile=:mobile", array(':uniacid' => $da['uniacid'], ':mobile' => $mobile));
                if (empty($mob)) {
                    pdo_insert('tg_sms_mobile', $data);
                }
                $Bdata = array(
                    'uniacid' => $_W['uniacid'],
                    'name' => $name,
                    'mobile' => $mobile,
                    'createtime' => time(),
                    'content' => $content,
                    'num' => $result['result']['fee'],
                    'status' => 2
                );
                pdo_insert('tg_sms_record', $Bdata);
            }
        }
    }
}

function nopay_success($openid, $price, $orderno, $goodsname, $time, $url)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_daipay = $setting['m_nocash'];
    $postdata = array(
        "first" => array("value" => "主人，我在购物车里都快发霉了，不仅要面对黑暗，还要遭受相思之苦，赶快付 款把我带走吧，人家想死你啦！", "color" => "#4a5077"),
        "type" => array('value' => '商品', "color" => "#4a5077"),
        "e_title" => array('value' => $goodsname, "color" => "#4a5077"),
        "o_id" => array('value' => $orderno, "color" => "#4a5077"),
        "order_date" => array('value' => $time, "color" => "#4a5077"),
        "o_money" => array('value' => "￥" . $price, "color" => "#4a5077"),
        "remark" => array("value" => '库存有限，商品卖完就没有了，快快完成支付吧！【一键支付】>>', "color" => "#4a5077"),
    );
    $content = "您有一个订单未支付！订单编号：" . $orderno . "下单时间：" . $time . "价格：" . $price . "库存有限，商品卖完就没有了，快快完成支付吧！";
    $ordno = pdo_fetch('select mobile,addname from ' . tablename('tg_order') . " where orderno='{$orderno}'");
    sendsms($content, 'm_smsnocash', $ordno['mobile'], $ordno['addname']);
    sendTplNotice($openid, $m_daipay, $postdata, $url);
}

function daipay_success($openid, $price, $name, $orderno, $goodsname, $time, $message, $url)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_daipay = $setting['m_daipay'];
    $postdata = array(
        "first" => array("value" => "代付成功啦！！！", "color" => "#4a5077"),
        "keyword1" => array('value' => $orderno, "color" => "#4a5077"),
        "keyword2" => array('value' => $name, "color" => "#4a5077"),
        "keyword3" => array('value' => "￥" . $price . "[" . $goodsname . "]", "color" => "#4a5077"),
        "keyword4" => array('value' => $time, "color" => "#4a5077"),
        "keyword5" => array('value' => $message, "color" => "#4a5077"),
        "remark" => array("value" => '点击查看详情', "color" => "#4a5077"),
    );
    $content = "您的朋友为您代付成功啦！订单编号：" . $orderno . "下单时间：" . $time . "价格：" . $price;
    $ordno = pdo_fetch('select mobile,addname from ' . tablename('tg_order') . " where orderno='{$orderno}'");
    sendsms($content, 'm_smsdaipay', $ordno['mobile'], $ordno['addname']);
    sendTplNotice($openid, $m_daipay, $postdata, $url);
}

function pro_change($openid, $title, $typename, $goodsname, $time, $message, $url)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_change = $setting['m_change'];
    $postdata = array(
        "first" => array("value" => $title, "color" => "#4a5077"),
        "keyword1" => array('value' => $typename, "color" => "#4a5077"),
        "keyword2" => array('value' => $goodsname, "color" => "#4a5077"),
        "keyword3" => array('value' => $time, "color" => "#4a5077"),
        "keyword4" => array('value' => $message, "color" => "#4a5077"),
        "remark" => array("value" => '点击查看详情', "color" => "#4a5077"),
    );
    //	$content=$title.$orderno."下单时间：".$time."价格：".$price;
    //$ordno=pdo_fetch('select mobile,addname from '.tablename('tg_order')." where orderno=':onrderno'",array(':orderno'=>$orderno));
    //sendsms($content,'m_change',$ordno['mobile'],$ordno['addname']);
    sendTplNotice($openid, $m_change, $postdata, $url);
}

function pay_onesuccess($orderno, $openid, $price, $ptime, $url)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_buy = $setting['m_buy'];
    $list = pdo_fetchall('select * from ' . tablename('tg_collect') . " where orderno='{$orderno}'");
    $ordno = pdo_fetch('select mobile,addname from ' . tablename('tg_order') . " where orderno='{$orderno}'");
    foreach ($list as $item) {
        $saler = pdo_fetch('select * from ' . tablename('tg_saler') . " where id='{$item['jishi_id']}'");
        $store = pdo_fetch('select * from ' . tablename('tg_store') . " where id='{$item['storeid']}'");
        $goods = pdo_get('tg_goods' , array('id' => $item['sid']));
        if ($goods['deliverytype'] == 1) {
            $address = pdo_get('tg_address' , array('id' => $item['address_id']));
            $key = '上门服务地址：' .$address['province'] ."-" .$address['city'] ."-" .$address['county'] ."-" .$address['detailed_address'];
        } else {
            $key = "门店：" . $store['storename'];
        }
        if ($_W['uniacid'] == 6 || $_W['uniacid'] == 4) {
            $postdata = array(
                "first" => array("value" => "您已成功预约," . $item['goodsname'], "color" => "#4a5077"),
                "keyword1" => array('value' => $item['goodsname'], "color" => "#4a5077"),
                "keyword2" => array('value' => date('Y-m-d H:i:s', $item['starttime']), "color" => "#4a5077"),
                "keyword3" => array('value' => $ordno['addname'], "color" => "#4a5077"),
                "keyword4" => array('value' => $ordno['mobile'], "color" => "#4a5077"),
                "keyword5" => array('value' => "课程：" . $store['storename'] . ",联系电话：" . $saler['mobile'], "color" => "#4a5077"),
                "remark" => array("value" => "请您按照约定时间" . date('Y-m-d H:i:s', $item['starttime']) . "，准时前往《" . $store['storename'] . "》", "color" => "#4a5077"),
            );
        } else {
            $postdata = array(
                "first" => array("value" => "您已成功预约," . $item['goodsname'], "color" => "#4a5077"),
                "keyword1" => array('value' => $item['goodsname'], "color" => "#4a5077"),
                "keyword2" => array('value' => date('Y-m-d H:i:s', $item['starttime']), "color" => "#4a5077"),
                "keyword3" => array('value' => $ordno['addname'], "color" => "#4a5077"),
                "keyword4" => array('value' => $ordno['mobile'], "color" => "#4a5077"),
                "keyword5" => array('value' => $key . ",技师:" . $saler['nickname'] . ",联系电话：" . $saler['mobile'], "color" => "#4a5077"),
                "remark" => array("value" => "", "color" => "#4a5077"),   //请您按照约定时间".date('Y-m-d H:i:s', $item['starttime'])."，准时前往《".$store['storename']."》
            );
        }

        //$content = "您已成功付款！订单编号：" . $orderno . "下单时间：" . date('Y-m-d H:i:s', $ptime) . "价格：" . $price;

        //sendsms($content, 'm_smsbuy', $ordno['mobile'], $ordno['addname']);
        sendTplNotice($openid, $m_buy, $postdata, $url);
    }
}

function pay_success($orderno, $openid, $neednum, $price, $goodsname, $url, $message)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_pay = $setting['m_pay'];

    $postdata = array(
        "first" => array("value" => "您已成功付款", "color" => "#4a5077"),
        "keyword1" => array('value' => $goodsname, "color" => "#4a5077"),
        "keyword2" => array('value' => $orderno, "color" => "#4a5077"),
        "keyword3" => array('value' => $neednum, "color" => "#4a5077"),

        "remark" => array("value" => $message, "color" => "#4a5077"),
    );
    $content = "您已成功付款！订单编号：" . $orderno . "下单时间：" . date('Y-m-d H:i:s', time());
    $ordno = pdo_fetch('select mobile,addname from ' . tablename('tg_order') . " where orderno='{$orderno}'");
    sendsms($content, 'm_smspay', $ordno['mobile'], $ordno['addname']);
    sendTplNotice($openid, $m_pay, $postdata, $url);
}

function choujian_success($orderno, $tuan_id, $openid, $goodsname, $url, $message)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_pay = $setting['m_tuan'];
    $tuan_first_order = pdo_fetch("select openid,g_id,address,mobile from" . tablename('tg_order') . "where godluck=1 and tuan_id='{$tuan_id}'");
    $profile = pdo_fetch("select nickname from" . tablename('mc_mapping_fans') . "where openid = '{$tuan_first_order['openid']}'");
    $postdata = array(
        "first" => array("value" => "恭喜您获得本次活动产品,我们将尽快为您发货", "color" => "#4a5077"),
        "keyword1" => array('value' => $goodsname, "color" => "#4a5077"),
        "keyword2" => array('value' => $profile['nickname'], "color" => "#4a5077"),
        "remark" => array("value" => $message, "color" => "#4a5077"),
    );
    $content = "恭喜您获得本次活动产品" . $goodsname . ",我们将尽快为您发货";
    $ordno = pdo_fetch('select mobile,addname from ' . tablename('tg_order') . " where orderno='{$orderno}'");
    sendsms($content, 'm_smstuan', $ordno['mobile'], $ordno['addname']);
    sendTplNotice($openid, $m_pay, $postdata, $url);
}

function group_success($tuan_id, $url)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_tuan = $setting['m_tuan'];
    $alltuan = pdo_fetchall("select openid,id,tuan_first,mobile,address,dispatchtype,orderno,tuan_id,addname from" . tablename('tg_order') . "where tuan_id = '{$tuan_id}' and mobile<>'虚拟' and status in(1,2,3,8)");
    $tuan_first_order = pdo_fetch("select openid,g_id,address,mobile from" . tablename('tg_order') . "where tuan_first=1 and tuan_id='{$tuan_id}'");
    $profile = pdo_fetch("select nickname from" . tablename('mc_mapping_fans') . "where openid = '{$tuan_first_order['openid']}'");
    $goods = pdo_fetch("select gname,selltype from" . tablename('tg_goods') . "where id = '{$tuan_first_order['g_id']}'");
    if ($goods['selltype'] == 2) {
        $contentb .= "团员:";
        foreach ($alltuan as $num => $all) {
            $pname = pdo_fetch("SELECT nickname FROM " . tablename('tg_member') . " WHERE uniacid ='{$_W['uniacid']}' and from_user = '{$all['openid']}'");
            $item[$num] = $all['id'];
            if ($all['tuan_first'] != 1) {
                $contentb .= $pname['nickname'] . ",联系电话:" . $all['mobile'] . "    ";
            }
            if ($all['tuan_first'] == 1) {
                $tuan_firstopenid = $all['openid'];
                $tuan_add = $all['address'];
                $tuan_tel = $all['mobile'];
            }
        }
        $contenta = "团长:" . $profile['nickname'] . "<br>";
        $content1 = $contentb . "\n" . "取件地址:" . $tuan_first_order['address'] . "\n" . "团长电话:" . $tuan_first_order['mobile'];
    } else {
        $content1 = "对于各位大侠的帮助，团长" . $profile['nickname'] . "感激涕零,我们一起坐等通知";
    }
    foreach ($alltuan as $key => $value) {
        if ($value['dispatchtype'] == 3) {
            $contents = "请于本品有效期内到店提货,点击查看自提二维码";
        } else {
            $contents = $content1;
        }
        /*团成功通知*/
        $url = app_url('order/group', array('tuan_id' => $value['tuan_id']));
        if ($value['dispatchtype'] == 3) {
            $url = app_url('order/order/detail', array('id' => $value['id'], 'b' => 1));
        }
        $postdata = array(
            "first" => array("value" => "恭喜组团成功   ！！！", "color" => "#4a5077"),
            "keyword1" => array('value' => $goods['gname'], "color" => "#4a5077"),
            "keyword2" => array('value' => $profile['nickname'], "color" => "#4a5077"),
            "remark" => array("value" => $contents, "color" => "#4a5077"),
        );
        $content = "恭喜" . $goods['gname'] . "组团成功,我们将尽快为您发货！";
        sendsms($content, 'm_smstuan', $value['mobile'], $value['addname']);
        sendTplNotice($value['openid'], $m_tuan, $postdata, $url);
    }
}

function hexiao_success($goodsname, $openid, $num, $hexiaotime, $url)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_hexiao = $setting['m_hexiao'];
    $postdata = array(
        "first" => array("value" => "亲，您的商品已发货!!!", "color" => "#4a5077"),
        "keyword1" => array('value' => $goodsname, "color" => "#4a5077"),
        "keyword2" => array('value' => $num, "color" => "#4a5077"),
        "keyword3" => array("value" => date('Y-m-d H:i:s', $hexiaotime), "color" => "#4a5077"),
        "remark" => array("value" => "", "color" => "#4a5077"),
    );
    sendTplNotice($openid, $m_hexiao, $postdata, $url);
}

function send_success($orderno, $openid, $express, $expressn, $url)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_send = $setting['m_send'];
    $postdata = array(
        "first" => array("value" => "亲，您的商品已发货!!!", "color" => "#4a5077"),
        "keyword1" => array('value' => $orderno, "color" => "#4a5077"),
        "keyword2" => array('value' => $express, "color" => "#4a5077"),
        "keyword3" => array("value" => $expressn, "color" => "#4a5077"),
        "remark" => array("value" => "", "color" => "#4a5077"),
    );
    $content = "亲，您的商品已发货！订单编号：" . $orderno . "快递公司：" . $express . "快递单号：" . $expressn . "请注意查收。";
    $ordno = pdo_fetch('select mobile,addname from ' . tablename('tg_order') . " where orderno='{$orderno}'");
    sendsms($content, 'm_smssend', $ordno['mobile'], $ordno['addname']);
    sendTplNotice($openid, $m_send, $postdata, $url);
}

function refund_success($orderno, $goodsname, $openid, $price, $ttime, $url)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_ref = $setting['m_ref'];
    $profile = pdo_fetch("select nickname from" . tablename('mc_mapping_fans') . "where openid = '{$openid}'");

    $postdata = array(
        "first" => array("value" => "您已退款成功！", "color" => "#4a5077"),
        "keyword1" => array('value' => $orderno, "color" => "#4a5077"),
        "keyword2" => array('value' => $goodsname, "color" => "#4a5077"),
        "keyword3" => array('value' => $profile['nickname'], "color" => "#4a5077"),
        "keyword4" => array('value' => "￥" . $price, "color" => "#4a5077"),
        "keyword5" => array('value' => date('Y-m-d H:i:s', $ttime), "color" => "#4a5077"),
        "remark" => array("value" => '点击查看订单处理详情', "color" => "#4a5077"),
    );
    $content = "您已成退款成功！订单编号：" . $orderno . "订单价格：" . $price . "退款时间：" . date('Y-m-d H:i:s', $ttime) . "请注意查看微信零钱。";
    $ordno = pdo_fetch('select mobile,addname from ' . tablename('tg_order') . " where orderno='{$orderno}'");
    sendsms($content, 'm_smsref', $ordno['mobile'], $ordno['addname']);
    sendTplNotice($openid, $m_ref, $postdata, $url);
}

//任务处理通知
function result_type($openid, $title, $message, $remark, $url)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_ref = $setting['m_result'];
    $postdata = array(
        "first" => array("value" => $title, "color" => "#4a5077"),
        "keyword1" => array('value' => $title, "color" => "#4a5077"),
        "keyword2" => array('value' => $message, "color" => "#4a5077"),
        "remark" => array("value" => $remark, "color" => "#4a5077"),
    );
    sendTplNotice($openid, $m_ref, $postdata, $url);
}

//补款通知
function bukuan_notice($openid, $title, $goodsname, $orderno, $datetime, $remark, $url)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_ref = $setting['m_bukuan'];
    $postdata = array(
        "first" => array("value" => $title, "color" => "#4a5077"),
        "keyword1" => array('value' => $goodsname, "color" => "#4a5077"),
        "keyword2" => array('value' => $orderno, "color" => "#4a5077"),
        "keyword3" => array('value' => $datetime, "color" => "#4a5077"),
        "remark" => array("value" => $remark, "color" => "#4a5077"),
    );
    sendTplNotice($openid, $m_ref, $postdata, $url);
}

/*
 * 参团人数不足通知
 * {{first.DATA}}
团购商品：{{keyword1.DATA}}
剩余拼团时间：{{keyword2.DATA}}
剩余拼团人数：{{keyword3.DATA}}
{{remark.DATA}}
 */
function no_num_success($openid, $title, $goodsname, $lasttime, $neednum, $remark, $url)
{
    global $_W;
    wl_load()->model('setting');
    $setting = setting_get_by_name("message");
    $m_ref = $setting['m_no_num_success'];
    $postdata = array(
        "first" => array("value" => $title, "color" => "#e4393c"),
        "keyword1" => array('value' => $goodsname, "color" => "#4a5077"),
        "keyword2" => array('value' => $lasttime, "color" => "#4a5077"),
        "keyword3" => array('value' => $neednum, "color" => "#4a5077"),
        "remark" => array("value" => $remark, "color" => "#4a5077"),
    );
    sendTplNotice($openid, $m_ref, $postdata, $url);
}

/*function cancelorder($openid, $price, $goodsname, $orderno,  $url) {
    global $_W;
    wl_load() -> model('setting');
    $setting = setting_get_by_name("message");
    $m_cancle = $setting['m_cancle'];
    $content = "取消订单通知";
    $postdata  = array(
                "first"=>array( "value"=> "取消订单通知","color"=>"#4a5077"),
                "keyword5"=>array('value' => "￥".$price."[未支付]", "color" => "#4a5077"),
                "keyword3"=>array('value' => $goodsname, "color" => "#4a5077"),
                "keyword2"=>array("value"=>$_W['uniaccount']['name'], "color" => "#4a5077"),
                "keyword1"=>array("value"=>$orderno, "color" => "#4a5077"),
                "keyword4"=>array("value"=>"1", "color" => "#4a5077"),
                "remark"=>array("value"=>"", "color" => "#4a5077"),
            );
       sendTplNotice($openid, $m_cancle, $postdata,$url);

}*/


?>